import scrapy

import time


class LiveCommentarySpider(scrapy.Spider):
    name = "live_commentary"
    start_urls = ["https://www.huya.com/wjz520cx"]

    def parse(self, response, **kwargs):
        # 提取主播名称
        anchor_name = response.xpath("//*[@id='J_roomHeader']/div[1]/div/div/h3/text()").extract_first()

        # 提取直播品类
        live_type = response.xpath("//*[@id='J_hostChannel']/a[2]/text()").extract_first()

        # 提取房间id
        room_id = response.css("#J_roomHeader > div.room-hd-l > div > div > span.host-rid > em::text").extract_first()

        # 提取直播热度
        live_hot = response.xpath("//em[@id='live-count']/text()").extract_first()

        # 直播间关注数量
        activityCount = response.css("#activityCount::text").extract_first()

        # 提取当前时间戳
        times = time.time()

        # 提取数据
        yield {
            'anchor_name': anchor_name,
            'live_type': live_type,
            'room_id': room_id,
            'live_hot': live_hot,
            'activityCount': activityCount,
            'times': times
        }

    @staticmethod
    def get_live_comment_information(response, **kwargs):
        # TODO: 获取直播间评论信息
        for i in response.css("#chat-room__list > div"):
            name = i.css("div > span.name.J_userMenu::text").extract_first()
            review = i.css("div > span.msg::text").extract_first()

            yield {
                'name': name,
                'review': review,
            }

# TODO: 创建一个新表,用来获取各个时间段的直播间热度指标
# TODO: 创建一个新表,用来获取各个时间段的直播间热度指标
